package crmdna.counter;

import com.googlecode.objectify.ObjectifyFilter;

import crmdna.client.Client;
import crmdna.common.APIException;
import crmdna.common.APIResponse.Status;

public class Counter {
	public enum CounterType {
		CHECKIN
	}

	public static void increment(String client, CounterType counterType,
			String key, int n) {
		Client.ensureValidClient(client);

		CounterCore counterCore = getCounterCore(counterType);
		counterCore.increment(client, getCounterName(counterType, key), n);
	}

	public static long incrementAndGetCurrentCount(String client,
			CounterType counterType, String key, int n) {
		increment(client, counterType, key, n);
		ObjectifyFilter.complete();

		return getCount(client, counterType, key);

	}

	public static long incrementAndGetCurrentCount(String client,
			CounterType counterType, String key) {
		return incrementAndGetCurrentCount(client, counterType, key, 1);
	}

	public static long getCount(String client, CounterType counterType,
			String key) {
		Client.ensureValidClient(client);

		CounterCore counterCore = getCounterCore(counterType);
		String counterName = getCounterName(counterType, key);
		long count = counterCore.getCount(client, counterName);

		return count;
	}

	private static String getCounterName(CounterType counterType, String key) {
		return counterType + "_" + key;
	}

	private static CounterCore getCounterCore(CounterType counterType) {
		// once counter impl is fixed for a type, shards can be increased but
		// never decreased
		if (counterType == CounterType.CHECKIN)
			return new CounterCore(2); // counter with 2 shards

		throw new APIException(Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED,
				"No implementation defined for counter type [" + counterType
						+ "]");
	}
}
